const handler = require('sv-handle-res')
const db = uniCloud.database()
const dbCmd = db.command

module.exports = {
  async recordList() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).get()

    return handler.result({
      data: recordRes.data,
    })
  },

  async recordAdd() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordParams = {
      user_id,
      fav_lib: [],
      fav_exam: [],
      buy_lib: [],
      error_exam: [],
      done_exam: [],
      correct_num: 0,
      error_num: 0
    }
    // 每注册一个用户，则生成唯一的用户信息记录表，在数据库中将user_id设置为唯一键
    const recordRes = await db.collection('st-user-record').add(recordParams)

    return handler.result({
      data: recordRes,
    })
  },

  // 收藏题库列表
  async favLibList() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_lib': true
    }).get()

    const favLibRes = await db.collection('st-exam-libs').where({
      'lib_id': dbCmd.in(recordRes.data[0].fav_lib)
    }).get();

    return handler.result({
      data: favLibRes.data,
      message: '查询成功'
    })
  },

  // 收藏题库添加
  async favLibAdd() {
    const {
      user_id,
      lib_id
    } = this.params

    if (!user_id || !lib_id) {
      throw handler.result({
        code: 40001
      })
    }

    const favLibRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_lib': true
    }).get()

    if (favLibRes.data[0].fav_lib.some(e => e === lib_id)) {
      throw handler.result({
        code: 40004,
        message: 'lib is already in fav_lib'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_lib': true
    }).update({
      "fav_lib": dbCmd.push(lib_id) // 向数组头部添加元素
    })

    return handler.result({
      data: recordRes,
      message: '添加成功'
    })
  },

  // 收藏题库移除
  async favLibDelete() {
    const {
      user_id,
      lib_id
    } = this.params

    if (!user_id || !lib_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const favLibRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_lib': true
    }).get()

    if (!favLibRes.data[0].fav_lib.some(e => e === lib_id)) {
      throw handler.result({
        code: 40004,
        message: 'lib is not in fav_lib'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id),
    }).field({
      'fav_lib': true
    }).update({
      "fav_lib": dbCmd.pull(lib_id)
    })

    return handler.result({
      data: recordRes,
      message: '删除成功'
    })
  },

  // 收藏题库清空
  async favLibClear() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id),
    }).field({
      'fav_lib': true
    }).update({
      "fav_lib": dbCmd.set([])
    })

    return handler.result({
      data: recordRes.data[0].fav_lib,
      message: '清空成功'
    })
  },

  // 已购题库列表
  async buyLibList() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      "buy_lib": true
    }).get()

    const buyLibRes = await db.collection('st-exam-libs').where({
      'lib_id': dbCmd.in(recordRes.data[0].buy_lib)
    }).get();

    return handler.result({
      data: buyLibRes.data,
      message: '查询成功'
    })
  },

  // 已购题库添加
  async buyLibAdd() {
    const {
      user_id,
      lib_id
    } = this.params

    if (!user_id || !lib_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const buyLibRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      "buy_lib": true
    }).get()

    if (buyLibRes.data[0].buy_lib.some(e => e === lib_id)) {
      throw handler.result({
        code: 40004,
        message: 'lib is already in buy_lib'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      "buy_lib": true
    }).update({
      "buy_lib": dbCmd.push(lib_id) // 向数组头部添加元素
    })

    return handler.result({
      data: recordRes,
      message: '添加成功'
    })
  },

  // 已购题库不提供移除接口，禁止删除已购记录

  // 收藏试题列表
  async favExamList() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_exam': true
    }).get()

    const favExamRes = await db.collection('st-exam-list').where({
      'exam_id': dbCmd.in(recordRes.data[0].fav_exam)
    }).get();

    return handler.result({
      data: favExamRes.data,
      message: '查询成功'
    })
  },

  // 收藏试题添加
  async favExamAdd() {
    const {
      user_id,
      exam_id
    } = this.params

    if (!user_id || !exam_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const favExamRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_exam': true
    }).get()

    if (favExamRes.data[0].fav_exam.some(e => e === exam_id)) {
      throw handler.result({
        code: 40004,
        message: 'exam is already in fav_exam'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_exam': true
    }).update({
      "fav_exam": dbCmd.push(exam_id) // 向数组头部添加元素
    })

    return handler.result({
      data: recordRes,
      message: '添加成功'
    })
  },

  // 收藏试题移除
  async favExamDelete() {
    const {
      user_id,
      exam_id
    } = this.params

    if (!user_id || !exam_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const favExamRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'fav_exam': true
    }).get()

    if (!favExamRes.data[0].fav_exam.some(e => e === exam_id)) {
      throw handler.result({
        code: 40004,
        message: 'exam is not in fav_exam'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id),
    }).field({
      'fav_exam': true
    }).update({
      "fav_exam": dbCmd.pull(exam_id)
    })

    return handler.result({
      data: recordRes,
      message: '删除成功'
    })
  },

  // 收藏试题清空
  async favExamClear() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id),
    }).field({
      'fav_exam': true
    }).update({
      "fav_exam": dbCmd.set([])
    })

    return handler.result({
      data: recordRes.data[0].fav_exam,
      message: '清空成功'
    })
  },

  // 错题列表
  async errorExamList() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_exam': true
    }).get()

    const errorExamRes = await db.collection('st-exam-list').where({
      'exam_id': dbCmd.in(recordRes.data[0].error_exam)
    }).get();

    return handler.result({
      data: errorExamRes.data,
      message: '查询成功'
    })
  },

  // 错题添加
  async errorExamAdd() {
    const {
      user_id,
      exam_id
    } = this.params

    if (!user_id || !exam_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const errorExamRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_exam': true
    }).get()

    if (errorExamRes.data[0].error_exam.some(e => e === exam_id)) {
      throw handler.result({
        code: 40004,
        message: 'exam is already in error_exam'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_exam': true
    }).update({
      "error_exam": dbCmd.push(exam_id) // 向数组头部添加元素
    })

    return handler.result({
      data: recordRes,
      message: '添加成功'
    })
  },

  // 错题移除
  async errorExamDelete() {
    const {
      user_id,
      exam_id
    } = this.params

    if (!user_id || !exam_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const errorExamRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_exam': true
    }).get()

    if (!errorExamRes.data[0].error_exam.some(e => e === exam_id)) {
      throw handler.result({
        code: 40004,
        message: 'exam is not in error_exam'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id),
    }).field({
      'error_exam': true
    }).update({
      "error_exam": dbCmd.pull(exam_id)
    })

    return handler.result({
      data: recordRes,
      message: '删除成功'
    })
  },

  // 错题清空
  async errorExamClear() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id),
    }).field({
      'error_exam': true
    }).update({
      "error_exam": dbCmd.set([])
    })

    return handler.result({
      data: recordRes.data[0].error_exam,
      message: '清空成功'
    })
  },

  // 已做题列表
  async doneExamList() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'done_exam': true
    }).get()

    const doneExamRes = await db.collection('st-exam-list').where({
      'exam_id': dbCmd.in(recordRes.data[0].done_exam)
    }).get();

    return handler.result({
      data: doneExamRes.data,
      message: '查询成功'
    })
  },

  // 已做题添加
  async doneExamAdd() {
    const {
      user_id,
      exam_id
    } = this.params

    if (!user_id || !exam_id) {
      throw handler.result({
        code: 40001,
      })
    }

    const doneExamRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'done_exam': true
    }).get()

    if (doneExamRes.data[0].done_exam.some(e => e === exam_id)) {
      throw handler.result({
        code: 40004,
        message: 'exam is already in done_exam'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'done_exam': true
    }).update({
      "done_exam": dbCmd.push(exam_id) // 向数组头部添加元素
    })

    return handler.result({
      data: recordRes,
      message: '添加成功'
    })
  },

  // 已做题不提供移除接口，禁止删除已做题记录

  // 刷题正确统计 - 只会累加，不提供减少和清0
  async correctNumQuery() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const numRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'correct_num': true
    }).get()

    return handler.result({
      data: numRes.data[0].correct_num,
    })
  },

  // 刷题正确+1
  async correctNumInc() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'correct_num': true
    }).update({
      'correct_num': dbCmd.inc(1)
    })

    const numRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'correct_num': true
    }).get()

    return handler.result({
      data: {
        ...recordRes,
        result: numRes.data[0].correct_num
      },
    })
  },

  // 刷题错误统计 - 只会累加，不提供减少和清0
  async errorNumQuery() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const numRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_num': true
    }).get()

    return handler.result({
      data: numRes.data[0].error_num
    })
  },

  // 刷题错误+1
  async errorNumInc() {
    const {
      user_id
    } = this.params

    if (!user_id) {
      throw handler.result({
        code: 40001,
        message: 'user_id is required'
      })
    }

    const recordRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_num': true
    }).update({
      'error_num': dbCmd.inc(1)
    })

    const numRes = await db.collection('st-user-record').where({
      "user_id": dbCmd.eq(user_id)
    }).field({
      'error_num': true
    }).get()

    return handler.result({
      data: {
        ...recordRes,
        result: numRes.data[0].error_num
      },
    })
  }
}